from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_monitor.entity.do.monitor_do import Monitor
from module_monitor.entity.vo.monitor_vo import MonitorModel, MonitorPageQueryModel
from utils.page_util import PageUtil


class MonitorDao:
    """
    监控管理模块数据库操作层
    """

    @classmethod
    async def get_monitor_detail_by_id(cls, db: AsyncSession, monitor_id: int):
        """
        根据获取监控管理详细信息

        :param db: orm对象
        :param monitor_id: 
        :return: 监控管理信息对象
        """
        monitor_info = (
            (
                await db.execute(
                    select(Monitor)
                    .where(
                        Monitor.monitor_id == monitor_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return monitor_info

    @classmethod
    async def get_monitor_detail_by_info(cls, db: AsyncSession, monitor: MonitorModel):
        """
        根据监控管理参数获取监控管理信息

        :param db: orm对象
        :param monitor: 监控管理参数对象
        :return: 监控管理信息对象
        """
        monitor_info = (
            (
                await db.execute(
                    select(Monitor).where(
                    )
                )
            )
            .scalars()
            .first()
        )

        return monitor_info

    @classmethod
    async def get_monitor_list(cls, db: AsyncSession, query_object: MonitorPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取监控管理列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 监控管理列表信息对象
        """
        query = (
            select(Monitor)
            .where(
                Monitor.device_id == query_object.device_id if query_object.device_id else True,
                Monitor.monitor_type == query_object.monitor_type if query_object.monitor_type else True,
                Monitor.file_url == query_object.file_url if query_object.file_url else True,
                Monitor.file_type == query_object.file_type if query_object.file_type else True,
                Monitor.file_name.like(f'%{query_object.file_name}%') if query_object.file_name else True,
            )
            .order_by(Monitor.monitor_id)
            .distinct()
        )
        monitor_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return monitor_list

    @classmethod
    async def add_monitor_dao(cls, db: AsyncSession, monitor: MonitorModel):
        """
        新增监控管理数据库操作

        :param db: orm对象
        :param monitor: 监控管理对象
        :return:
        """
        db_monitor = Monitor(**monitor.model_dump(exclude={}))
        db.add(db_monitor)
        await db.flush()

        return db_monitor

    @classmethod
    async def edit_monitor_dao(cls, db: AsyncSession, monitor: dict):
        """
        编辑监控管理数据库操作

        :param db: orm对象
        :param monitor: 需要更新的监控管理字典
        :return:
        """
        await db.execute(update(Monitor), [monitor])

    @classmethod
    async def delete_monitor_dao(cls, db: AsyncSession, monitor: MonitorModel):
        """
        删除监控管理数据库操作

        :param db: orm对象
        :param monitor: 监控管理对象
        :return:
        """
        await db.execute(delete(Monitor).where(Monitor.monitor_id.in_([monitor.monitor_id])))

